package com.example.mooctao;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.Random;
//注册页面
public class  RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText phone_edit;
    private EditText password;
    private EditText code_edit;
    private Button code;
   private Button register;
   private String verifyCode;
    private String passwords;
//创建视图
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        phone_edit=findViewById(R.id.phonenum_edit);
        password= findViewById(R.id.passwordnum_build);
        code_edit=findViewById(R.id.getcode);
        code=findViewById(R.id.code_btn);
        register=findViewById(R.id.send_btn);
        password.addTextChangedListener(new HideTextWatcher(password,6));
        phone_edit.addTextChangedListener(new HideTextWatcher(phone_edit,11));
        code_edit.addTextChangedListener(new HideTextWatcher(code_edit,6));
        code.setOnClickListener(this);
        register.setOnClickListener(this);
        //点击叉号图标退出当前界面
        findViewById(R.id.back_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });


    }

    @Override
    public void onClick(View view) {
        String phone=phone_edit.getText().toString();
         passwords=password.getText().toString();
        if(phone.length()<11){
            Toast.makeText(this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
            return;
        }
        if(passwords.length()<6){
            Toast.makeText(this,"请输入正确的密码",Toast.LENGTH_SHORT).show();
            return;
        }
        switch (view.getId()){
            //获取验证码
            case R.id.code_btn:
                verifyCode = String.format("%06d", new Random().nextInt(999999));
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setTitle("请记住验证码");
                builder.setMessage("手机号" + phone + "，本次验证码是" + verifyCode + ",请输入验证码");
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
                //点击注册按钮
            case R.id.send_btn:
                if(!verifyCode.equals(code_edit.getText().toString())){
                    Toast.makeText(this,"请输入正确的验证码",Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show();

                //提示用户注册成功
                Intent intent=new Intent();
                intent.putExtra("re_password",passwords);
                intent.putExtra("re_phone", phone);  // 传递手机号
                setResult(Activity.RESULT_OK, intent);  // 设置返回结果
                finish();
                break;
        }

    }

//文本输入键盘隐藏
    private class HideTextWatcher implements TextWatcher {
        private EditText mView;
        private int mMaxLength;
        private Context mContext;
        public HideTextWatcher(EditText v, int maxLength) {
            this.mView=v;
            this.mMaxLength=maxLength;
            this.mContext = RegisterActivity.this;  // 获取当前Activity的Context
        }

        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void afterTextChanged(Editable editable) {
            if(editable.toString().length()==mMaxLength){
                //隐藏键盘
                InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null && mView != null) {
                    imm.hideSoftInputFromWindow(mView.getWindowToken(), 0);
                }
            }

        }
    }

}
/*
RegisterActivity被加载时，onCreate方法被调用，设置布局并初始化UI组件。
用户在输入框中输入手机号码、密码和验证码。
用户点击验证码按钮，生成一个随机验证码并显示对话框。
用户点击注册按钮，onClick方法被调用，验证所有输入。
如果所有输入验证通过，显示注册成功的提示，并通过Intent返回密码和手机号码。
如果输入不正确，显示相应的错误提示。
 */